﻿using CPPEI.ETL.DataServiceInterface;
using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;
using SER = CPPEI.ETL.Domain.Services;
using VIW = CPPEI.ETL.Domain.Views;

namespace CPPEI.ETL.WebApi
{
    [CPPEI.WebApi.AuthGetAction("BATCH", "GetDataList", "1.0")]
    public class GetDataListAction : BaseAction<GetDataListParams, GetDataListResult>
    {
        protected override ActionResult<GetDataListResult> OnExecute(ActionContext context,
            SysParameters sysParameters,
            GetDataListParams opParams)
        {
            SER.BatchService service = new SER.BatchService();
            ServiceResult<MOD.MDtTableData> serResult = service.GetData(opParams.BatchId, opParams.Status, opParams.Page * 25, 25);
            if (serResult.IsSuccess)
            {
                GetDataListResult actResult = new GetDataListResult();
                actResult.Page = opParams.Page;
                actResult.Start = serResult.Data.Start;
                actResult.Limit = serResult.Data.Limit;
                actResult.Total = serResult.Data.Total;
                actResult.Count = serResult.Data.DataTable.Rows.Count;

                List<string> columns = new List<string>();
                List<string> types = new List<string>();
                for (int colIndex = 0; colIndex < serResult.Data.DataTable.Columns.Count; colIndex++)
                {
                    columns.Add(serResult.Data.DataTable.Columns[colIndex].ColumnName);
                    types.Add(serResult.Data.DataTable.Columns[colIndex].DataType.FullName);
                }
                actResult.Columns = columns;
                actResult.Types = types;

                for (int rowIndex = 0; rowIndex < serResult.Data.DataTable.Rows.Count; rowIndex++)
                {
                    System.Data.DataRow dataRow = serResult.Data.DataTable.Rows[rowIndex];
                    GetDataListResult.Item item = new GetDataListResult.Item();

                    for (int colIndex = 0; colIndex < actResult.Columns.Count; colIndex++)
                    {
                        item.Add(Convert.ToString(dataRow[actResult.Columns[colIndex]]));
                    }

                    actResult.Items.Add(item);
                }

                return new ActionResult<GetDataListResult>(actResult);
            }
            else
            {
                return new ActionResult<GetDataListResult>(serResult.IsSuccess, serResult.ErrorCode, serResult.Message, null);
            }
        }
    }

    public class GetDataListParams
    {
        public Guid BatchId { get; set; }

        /// <summary>
        ///  1数据类型不匹配,2数据审核失败,3编码转换失败,4等待数据审核,5插入数据失败,6插入数据成功
        /// </summary>
        public int Status { get; set; }

        [Default(0)]
        public int Page { get; set; }
    }

    public class GetDataListResult : PagedResult
    {
        public List<string> Columns { get; set; }
        public List<string> Types { get; set; }
        public List<Item> Items = new List<Item>();

        public class Item
        {
            public List<string> Values { get; set; }

            public Item()
            {
                Values = new List<string>();
            }

            public void Add(string value)
            {
                Values.Add(value);
            }
        }
    }
}